// pages/campus-abbr/campus-abbr.js'
let searchDisabled = false
let userInfo = {}
let app = getApp()
var req = require("../../utils/action.js");
var utils = require("../../utils/util.js");
var ifSearch = false
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showSidebar:true,
    itemList: [],
    keyword: '',
    loading: true,
    pageNo:0,
    pageSize:10,
    complete:false,
    userInfo:{my:"?"}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    let userInfo = await app.globalData.userInfo
    console.log(userInfo)
    if(!(userInfo.email || (userInfo.img && userInfo.img.length > 0))){
      wx.showModal({
        title: '提示',
        content: '您好未注册，是否注册页面先进行注册？',
        success: function (res) {
          if (res.confirm) {
            console.log('确定')
            wx.navigateTo({
              url: '/pages/profile-edit/profile-edit',
            })
          } else {
            console.log('取消')
          }
        }
      })
    }
    this._loadItemList(0)
    this.setData({userInfo:userInfo})
  },
  check(that){
    
  },
  bindKeyInput(e){
    this.setData({keyword:e.detail.value})
    console.log(this.data.keyword)
  },

  showSelectWin(){
    this.setData({popup:true})
  },
  bindTextAreaBlur: function(e) {
    console.log(e.detail.value)
    this.setData({reportReason:e.detail.value})
  },
  modalCandel(){
    that.setData({showModelWin:false,reportReason:'',reportId:''})
  },
  
  _loadItemList(type) {
    console.log("_loadItemList_loadItemList_loadItemList_loadItemList")
    this.setData({
      loading: true
    }, ()=>{
      let data = {
        "pageNo":this.data.pageNo,
        "pageSize":this.data.pageSize,
      },
      that = this
      data.keyword = this.data.keyword
      data.schoolId = app.globalData.userInfo.schoolId
      data.searchType = "all"
      console.log("data:"+JSON.stringify(data))
      let _url = req.actions.ym+ "invitation/pageList"
      utils.myResquest(data,_url,function(res){
        console.log("_loadItemList===>"+JSON.stringify(res))
        let rows = res.data.result.records
        that.setData({
          complete:rows && rows.length < 20,
          itemList: type == 0 ? rows : that.data.itemList.concat(rows),
          loading:false
        })
      })
      wx.stopPullDownRefresh()
    })
  },
  onClickSearch() {
    let keyword = this.data.keyword
    if(keyword.trim() == '') {
      wx.showToast({
        title: '搜索关键字不能为空',
        icon: 'none'
      })
      return
    } 
    ifSearch = true
    this._loadItemList(0)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: async function () {
    this.getTabBar().init()
    searchDisabled = false
    userInfo = await getApp().globalData.userInfo
    console.log(userInfo)
    // 将用户名存入本地数据
    var storageInfo={"avatar":userInfo["myAvatarUrl"],"username":userInfo["myNickName"]}
    wx.setStorageSync('userInfo', storageInfo)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    searchDisabled = false
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({pageNo:0,itemList:[]})
    this._loadItemList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.complete) {
      wx.showToast({
        title: '已全部加载~',
        icon: 'none'
      })
      return
    }
    //this._loadItemList(this.data.itemList.length)
    this.setData({pageNo:this.data.pageNo+1})
    this._loadItemList()
  },

  parentOperation(e){
    console.log(e)
    let itemList = this.data.itemList
    if(e.detail.type == 'deleteInvitation'){
      for(var i=0;i<itemList.length;i++){
        if(itemList[i].id == e.detail.id){
          itemList[i].hidden = true
          this.setData({itemList:itemList})
          break;
        }
      }
    }else{
      for(var i=0;i<itemList.length;i++){
        if(itemList[i].id == e.detail.id){
          itemList[i].over = true
          this.setData({itemList:itemList})
          break;
        }
      }
    }
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '这个很有趣，一起看看吧',
      path: `/pages/campus/campus`,
    }
  },
  onChange(event) {
    console.log(event.currentTarget.dataset.id)
    ifSearch = false
    if(this.data.activeClass == event.currentTarget.dataset.id)
      return
    this.setData({
      activeClass:event.currentTarget.dataset.id,
      activeSubClass: 0
    }, ()=>{
      this._loadItemList(0)
    })
  },
  onChangeSub(event) {
    ifSearch = false
    if(this.data.activeSubClass == event.detail)
      return
    this.setData({
      activeSubClass:event.detail
    }, ()=>{
      this._loadItemList()
    })
  },
  async onClickPublish() {
    let name = app.globalData.userInfo.name,userInfo = app.globalData.userInfo
    if (!searchDisabled) {
      searchDisabled = true
      if (!name) {
        wx.showToast({
          title: "请先去[我的->个人信息]界面完成身份验证~",
          icon: 'none'
        })
        searchDisabled = false
      } else if (!(userInfo.if_valid == 'Y')) {
        wx.showToast({
          title: "请等待身份验证通过~",
          icon: 'none'
        })
        searchDisabled = false
      } else {
        wx.navigateTo({
          url: `../campus-edit/campus-edit`,
          success: function(res) {
            // 通过eventChannel向被打开页面传送数据
            res.eventChannel.emit('acceptDataFromOpenerPage', { data: userInfo })
          }
        })
        searchDisabled = false
      }
    }
  },
  goComment(event) {
    console.log("goComment")
    console.log(event)
    wx.navigateTo({
      url: '../../pages/campus-comment/campus-comment?type=0&itemId='+event.currentTarget.dataset.itemId,
      success: function(res) {
        // 通过eventChannel向被打开页面传送数据
        console.log(event.currentTarget.dataset.item)
        res.eventChannel.emit('acceptDataFromOpenerPage', { 
          data: event.currentTarget.dataset.item
        })
      }
    })
  },
  ChangeSidebar(){
    this.setData({"showSidebar":!this.data.showSidebar})
    console.log('taps',this.data.showSidebar)
  }
})